#!/bin/bash
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

TOOL_DIR=$(cd "$(dirname "$0")" || exit ; pwd)
WORKSPACE_DIR=$(dirname "$TOOL_DIR")

TABLE_SPACE="tab_ehuser"
ORIGIN_FILE=$1
TARGET_DIR=$2
DB_TYPE=$3

if [ "$TABLE_SPACE" =  "" ]; then
    printf "[ERROR] The table space not found in command "
    exit 2
fi
if [ "$ORIGIN_FILE" =  "" ]; then
    printf "[ERROR] The origin file path not found in command"
    exit 2
fi
if [ "$TARGET_DIR" =  "" ]; then
    printf "[ERROR] The target file path not found in command "
    exit 2
fi
if [ "$DB_TYPE" =  "" ]; then
    printf "[ERROR] The db type path not found in command "
    exit 2
fi


"${JAVA_HOME}/bin/java" -Dfile.encoding=UTF-8 -jar "${WORKSPACE_DIR}"/tools/transfersql-commandline-1.0-SNAPSHOT.jar -tableSpace="$TABLE_SPACE" -originFile="$ORIGIN_FILE" -targetFile="$TARGET_DIR" -dbType="$DB_TYPE"
printf "transfersql complete . \n"

rsync -av --delete "$TARGET_DIR/" "$ORIGIN_FILE/"
printf "copy file to origin dir success .\n"

rm -rf "$TARGET_DIR"
printf "finish success. \n"